<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2022/5/23
  Time: 15:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="header.jsp"%>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <div class="demoTable">
        ID：
        <div class="layui-inline">
            <input class="layui-input" name="id" id="searchId" autocomplete="off">
        </div>
        名字：
        <div class="layui-inline">
            <input class="layui-input" name="name" id="searchName" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>

    <table class="layui-hide" id="test" lay-filter="test"></table>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-sm" lay-event="deleteAll">批量删除</button>
        </div>
    </script>

    <script>
        layui.use('table', function(){
            var table = layui.table;
            table.render({
                elem: '#test'
                ,url:'/banji/selectByPage'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    ,layEvent: 'LAYTABLE_TIPS'
                    ,icon: 'layui-icon-tips'
                }]
                ,title: '用户数据表'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'id', title:'ID', sort: true}
                    ,{field:'name', title:'班级名'}
                    ,{field:'faculty', title:'院系名'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                ]]
                ,page: true
                ,id: 'tableId'
            });

            //头工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'add':
                        // 弹出添加界面
                        layer.open({
                            type: 2,
                            area: ['700px','270px'],
                            content: '/banji/getBanjiAddPage'
                        });
                        break;
                    case 'deleteAll':
                        var data = checkStatus.data;
                        console.log(data);
                        // [{id: 10, name: '张三10', age: 3, gender: '男'},{id: 16, name: '7', age: 8, gender: '女'}]
                        var idArray = []; // [10,16]
                        for (var i = 0; i < data.length; i++) {
                            idArray.push(data[i].id);
                        }
                        // [10,16] ->  '10,16'
                        var ids = idArray.join(',')

                        layer.confirm('真的要删除么？', function() {
                            $.post(
                                '/banji/deleteAll',
                                {'ids': ids}, // '10,16'
                                function(jsonResult) {
                                    console.log(jsonResult);
                                    // 这个后台操作是成功
                                    if (jsonResult.ok) {
                                        layer.msg(jsonResult.msg);
                                    }
                                    // 删除完了之后刷新表格，展示最新的数据
                                    table.reload('tableId');
                                },
                                'json'
                            );
                        });

                        break;
                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){
                // data就代表当前这一行的Banji对象
                var data = obj.data;
                //console.log(obj)
                if(obj.event === 'del'){
                    layer.confirm('真的要删除么？', function() {
                        $.post(
                            '/banji/deleteById',
                            {'id': data.id},
                            function(jsonResult) {
                                console.log(jsonResult);
                                // 这个后台操作是成功
                                if (jsonResult.ok) {
                                    layer.msg(jsonResult.msg);
                                }
                                // 删除完了之后刷新表格，展示最新的数据
                                table.reload('tableId');
                            },
                            'json'
                        );
                    });
                } else if(obj.event === 'edit'){
                    // 弹出编辑界面
                    layer.open({
                        type: 2,
                        area: ['700px','270px'],
                        content: '/banji/getBanjiUpdatePage?id=' + data.id
                    });
                }
            });

            // 搜索
            var $ = layui.$, active = {
                reload: function(){
                    var demoReload = $('#demoReload');

                    //执行重载
                    table.reload('tableId', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            // 得到输入框里面输入的搜索条件
                            // localhost:8080/banji/selectByPage?page=1&limit=10&id=9&name=zs
                            id: $('#searchId').val(),
                            name:$('#searchName').val()
                        }
                    });
                }
            };

            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>

    <%--${list}--%>
    <%--<table border="1" cellspacing="0"  style="width: 800px">
        <tr>
            <td>ID</td>
            <td>名字</td>
            <td>年龄</td>
            <td>性别</td>
            <td>删除</td>
        </tr>
        <c:forEach items="${list}" var="banji">
            <tr>
                <td>${banji.id}</td>
                <td>${banji.name}</td>
                <td>${banji.age}</td>
                <td>${banji.gender}</td>
                <td><a href="/banji/deleteById?id=${banji.id}">删除</a></td>
            </tr>
        </c:forEach>
    </table>--%>
</body>
</html>
